package com.ytclub.auth.application.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.google.common.base.Preconditions;
import com.ytclub.auth.application.convent.AuthRoleDTOConverter;
import com.ytclub.auth.application.dto.AuthRoleDTO;
import com.ytclub.auth.domain.entity.AuthRoleBO;
import com.ytclub.auth.domain.service.AuthRoleDoaminService;
import com.ytclub.auth.entity.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 角色控制层类
 *
 * @author: HuangQi
 * @date: Created in 17:10 2024/3/23
 * @description: 角色控制层
 */
@RestController
@RequestMapping("/role/")
@Slf4j
public class RoleController {

    @Resource
    private AuthRoleDoaminService authRoleDoaminService;


    /**
     * 新增角色
     *
     * @param authRoleDTO 新增角色的信息
     * @return 新增结果
     */
    @RequestMapping("add")
    public Result<Boolean> add(@RequestBody AuthRoleDTO authRoleDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("RoleController.add.dto:{}", JSON.toJSONString(authRoleDTO));
            }
            // 参数校验
            Preconditions.checkArgument(!StringUtils.isBlank(authRoleDTO.getRoleKey()), "角色唯一标识不能为空");
            Preconditions.checkArgument(!StringUtils.isBlank(authRoleDTO.getRoleName()), "角色名称不能为空");

            // 调用领域层
            AuthRoleBO authRoleBO = AuthRoleDTOConverter.INSTANCE.convertDTOToBO(authRoleDTO);
            Boolean registerResult = authRoleDoaminService.add(authRoleBO);

            // 返回结果
            return Result.ok(registerResult);
        } catch (Exception e) {
            // 异常处理
            log.error("RoleController.add.error:{}", e.getMessage(), e);
            return Result.fail("注册角色失败");
        }
    }

    /**
     * 更新角色
     *
     * @param authRoleDTO 更新角色的信息
     * @return 更新结果
     */
    @RequestMapping("update")
    public Result<Boolean> update(@RequestBody AuthRoleDTO authRoleDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("RoleController.update.dto:{}", JSON.toJSONString(authRoleDTO));
            }
            // 参数校验
            Preconditions.checkNotNull(authRoleDTO.getId(), "角色id不能为空");


            // 调用领域层
            AuthRoleBO authRoleBO = AuthRoleDTOConverter.INSTANCE.convertDTOToBO(authRoleDTO);
            Boolean registerResult = authRoleDoaminService.update(authRoleBO);

            // 返回结果
            return Result.ok(registerResult);
        } catch (Exception e) {
            // 异常处理
            log.error("RoleController.update.error:{}", e.getMessage(), e);
            return Result.fail("注册角色失败");
        }
    }


    /**
     * 删除角色(逻辑删除)
     *
     * @param authRoleDTO 删除角色的信息
     * @return 删除结果
     */
    @RequestMapping("delete")
    public Result<Boolean> delete(@RequestBody AuthRoleDTO authRoleDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("RoleController.delete.dto:{}", JSON.toJSONString(authRoleDTO));
            }
            // 参数校验
            Preconditions.checkNotNull(authRoleDTO.getId(), "角色id不能为空");


            // 调用领域层
            AuthRoleBO authRoleBO = AuthRoleDTOConverter.INSTANCE.convertDTOToBO(authRoleDTO);
            Boolean registerResult = authRoleDoaminService.delete(authRoleBO);

            // 返回结果
            return Result.ok(registerResult);
        } catch (Exception e) {
            // 异常处理
            log.error("RoleController.delete.error:{}", e.getMessage(), e);
            return Result.fail("注册角色失败");
        }
    }


}
